package com.ldy.core.web;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.core.common.Result;
import com.ldy.core.entity.BaseOrg;
import com.ldy.core.service.BaseOrgService;

/**
 * 机构管理
 */
@RestController
@RequestMapping("/org")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class OrgController {
	
	private static final Logger logger = LoggerFactory.getLogger(OrgController.class);
	
	@Autowired
	private BaseOrgService orgService;

	/**
	 * 列表分页查询
	 * @param current
	 * @param size
	 * @param app
	 * @return
	 */
	@PostMapping("/selectPage")
	public Result selectPage(int current, int size, BaseOrg app) {
		Page<BaseOrg> page = new Page<BaseOrg>(current, size);
		QueryWrapper<BaseOrg> query = new QueryWrapper<>();
		if (StringUtils.hasText(app.getFullName())) {
			query.like("full_name", app.getFullName());
		}
		if (StringUtils.hasText(app.getOrgCode())) {
			query.like("org_code", app.getOrgCode());
		}
		if (StringUtils.hasText(app.getCreditCode())) {
			query.like("credit_code", app.getCreditCode());
		}
		if (StringUtils.hasText(app.getOrgClassification())) {
			query.eq("org_classification", app.getOrgClassification());
		}
		query.orderByDesc("create_time");
		return Result.success(orgService.page(page, query));
	}
	
	/**
	 * 列表查询
	 * @return
	 */
	@PostMapping("/selectList")
	public Result selectList(BaseOrg vo) {
		return Result.success(orgService.list(new QueryWrapper<>(vo)));
	}
	
	/**
	 * 新增
	 * 
	 * @return
	 */
	@PostMapping("/insert")
	public Result insert(BaseOrg org) {
		logger.info("method: {}, params:[org: {}]", "insert", JSONObject.toJSONString(org));
		Date now = new Date();
		org.setCreateTime(now);
		org.setLastUpdateTime(now);
		orgService.save(org);
		orgService.refreshCacheOrgs();
		return Result.success();
	}
	
	/**
	 * 获取本机及下级的 orgCode
	 * @return
	 */
	@PostMapping("/getSubOrgs")
	public Result getSubOrgs () {
		return Result.success(orgService.getMySubOrgs());
	}
	
	/**
	 * 更新
	 * 
	 * @return
	 */
	@PostMapping("/update")
	public Result update(BaseOrg org) {
		logger.info("method: {}, params:[org: {}]", "update", JSONObject.toJSONString(org));
		Date now = new Date();
		org.setLastUpdateTime(now);
		orgService.updateById(org);
		orgService.refreshCacheOrgs();
		return Result.success();
	}
	
	/**
	 * 删除
	 * 
	 * @return
	 */
	@PostMapping("/delete")
	public Result delete(String id) {
		logger.info("method: {}, params:[id: {}]", "delete", id);
		orgService.removeById(id);
		orgService.refreshCacheOrgs();
		return Result.success();
	}
}
